//
//  gwcxxqViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2017/4/19.
//  Copyright © 2017年 czd. All rights reserved.
//

#import "gwcxxqViewController.h"
#import "ckUnDonelistCell.h"
#import "daibanTableViewCell.h"

@interface gwcxxqViewController ()
@property(nonatomic,assign)NSInteger currentPage; //当前页
@property(nonatomic,assign)NSInteger totalCounts; //总条数
@property(nonatomic,strong)NSDictionary * adic;
@property(nonatomic,strong)NSMutableArray * dataArray;

@end

@implementation gwcxxqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.title = @"查询结果";
         _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];

         self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    
    [self.view addSubview:_tableView];
                 __weak typeof(self) weakSelf = self;

              [self.tableView addHeaderWithCallback:^{
        weakSelf.currentPage = 1;
    [weakSelf loadSearchData:_searchText];

   
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){

    [weakSelf loadsearchMore];

        }else{
        
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:@"没有更多了"];
    
        }
       
    }];
    // Do any additional setup after loading the view.
}
-(void)loadSearchData:(NSString*)Searchtext{
//
    __weak typeof(self) weakSelf = self;
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
_currentPage = 1;
    NSArray * array = @[@{@"condition":_searchText},@{@"dbpath": _searchyear},@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];
NSLog(@"%@",array);
 [HttpTools requestWithURL:KexexchSearchURL arrays:array method:@"ArchiveYearSearch" success:^(id JSON) {
  if (self.currentPage ==1) {
            [self.dataArray removeAllObjects];
        }
        _adic= JSON[@"display"];

      _dataArray =   [NSMutableArray array];
        NSArray *array = JSON[@"data"];
  self.totalCounts =[JSON[@"number"] intValue];

if(JSON[@"number"]){

       self.title = [NSString stringWithFormat:@"查询结果(%@)",JSON[@"number"] ];}

       else{

       self.title = @"查询结果";
       }         for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
        [weakSelf.tableView footerEndRefreshing];
        [weakSelf.tableView reloadData];
        weakSelf.currentPage++;
     
        if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }
     
    } failure:^(NSError *error) {
        if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }
    }];
    




}
- (void)loadsearchMore{
    __weak typeof(self) weakSelf = self;
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

    NSArray * array = @[@{@"condition":_searchText},@{@"dbpath": _searchyear},@{@"userId":userId},@{@"page":[NSString stringWithFormat:@"%ld",(long)self.currentPage]}];

 [HttpTools requestWithURL:KexexchSearchURL arrays:array method:@"ArchiveYearSearch" success:^(id JSON) {
                NSArray *array = JSON[@"data"];

 for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
_adic= JSON[@"display"];
//        self.totalCounts =[JSON[@"number"] intValue];
//
//       self.title = [NSString stringWithFormat:@"公文查询(%@)",JSON[@"number"] ];
    self.currentPage++;
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if (self.tableView.headerRefreshing) {
            [self.tableView headerEndRefreshing];
        }
        if (self.tableView.footerRefreshing) {
            [self.tableView footerEndRefreshing];
        }
    }];
    




}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UnDoneModel * model = self.dataArray[indexPath.row];
//    CGFloat height = 0;
//    CGFloat  titlesize  = [OAToolBox sizebyText:model.title font:14 minsWidth:80];
//
//    NSString * desstring = [NSString stringWithFormat:@"%@",model.time];
//    CGFloat dessize = [OAToolBox sizebyText:desstring font:13 minsWidth:40];;
//    height = titlesize + dessize + 20;
//    return  height;
    NSDictionary *dic = self.dataArray[indexPath.row];

    CGFloat  titleFloat  = [OAToolBox sizebyText:dic[@"subject"] font:15 minsWidth:60];

    return  titleFloat  +10+10+20;
}




#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *CellIdentifier = @"Cell";

    daibanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
  if (cell==nil) {
        cell=[[daibanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    cell.adic = _adic;
    cell.number = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    return cell;




//    OAUnDoneListCell * cell = [OAUnDoneListCell cellWithTableView:tableView];
//    UnDoneModel * model = self.dataArray[indexPath.row];
//    cell.model = model;
//    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary *dic = self.dataArray[indexPath.row];

     UnDoneModel * model = self.dataArray[indexPath.row];
     detailViewController * detailVC = (detailViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"detail"];
detailVC.fileId = dic[@"unid"];
detailVC.type = @"chaxun";
detailVC.year =  _searchyear;

[self.navigationController pushViewController:detailVC animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
